﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Business;
using System.Globalization;
namespace ServicesDirectory.UserControl
{
    public partial class LookupContactManager : System.Web.UI.UserControl
    {


        public event EventHandler GetManager;
        public event EventHandler CloseControl;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string message;
                var lstContact = ContactBusiness.Instance.ListContact(string.Empty, string.Empty, !chkInactive.Checked, out message);
                if (lstContact != null)
                {
                    lblTotalPage.Text = lstContact.Count % 15 == 0 ? (lstContact.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstContact.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture), 15, lstContact);
                    rptContacts.DataBind();
                }
                else
                {
                    lblMessage.Text = message;
                    lblMessage.Visible = true;
                }
            }
            btnClear.Click += (Clear_Click);
        }

        void Clear_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = string.Empty;
            txtSurname.Text = string.Empty;
            chkInactive.Checked = false;
        }

        /// <summary>
        /// Handles the Click event of the btnSelect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void BtnSelect_Click(object sender, EventArgs e)
        {

            var selectContact = false;
            foreach (RepeaterItem item in rptContacts.Items)
            {

                var rdo = (RadioButton)item.FindControl("rbtSelect");
                var contactId = item.FindControl("hdContactId") as HiddenField;
                if (rdo.Checked)
                {
                    selectContact = true;
                    if (contactId != null)
                    {
                        if (GetManager != null)
                            GetManager(contactId.Value, e);

                    }

                    break;
                }
            }
            if (!selectContact)
            {
                lblMessage.Text = "you have to choose a row";
            }
            else
            {

                if (CloseControl != null)
                    CloseControl(sender, e);
            }
        }


        void Search()
        {
            string message;
            var lstContact = ContactBusiness.Instance.ListContact(txtFirstName.Text, txtSurname.Text, !chkInactive.Checked, out message);
            if (lstContact != null)
            {
                rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture), 15, lstContact);

                rptContacts.DataBind();
            }
            else
            {
                Response.Write("<script type=\"text/javascript\">alert('" + message + "');</script>");
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdSearchContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void SearchContact_Click(object sender, EventArgs e)
        {
            Search();

        }

        /// <summary>
        /// Handles the ItemDataBound event of the rptContacts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        protected void Contacts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var lblIsactive = e.Item.FindControl("lblIsactive") as Label;
            if (lblIsactive != null)
                if (lblIsactive.Text == "True")

                    lblIsactive.Text = "Yes";
                else
                {
                    lblIsactive.Text = "No";
                }
            var rdo = (RadioButton)e.Item.FindControl("rbtSelect");
            const string script = "SetUniqueRadioButton(this)";
            rdo.Attributes.Add("onclick", script);


        }

        /// <summary>
        /// Handles the CheckedChanged event of the chkInActive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Inactive_CheckedChanged(object sender, EventArgs e)
        {
            Search();
        }



        /// <summary>
        /// Handles the Click event of the btnNone control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void None_Click(object sender, EventArgs e)
        {
            if (GetManager != null)
                GetManager(Guid.Empty, e);
            if (CloseControl != null)
                CloseControl(sender, e);
        }

        /// <summary>
        /// Handles the Click event of the btnClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Close_Click(object sender, EventArgs e)
        {
            if (CloseControl != null)
                CloseControl(sender, e);

        }

        /// <summary>
        /// Handles the Click event of the btnBackAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void BackAll_Click(object sender, EventArgs e)
        {
            tbPageNumber.Text = "1";
            Search();

        }

        /// <summary>
        /// Handles the Click event of the btnBackList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Backlist_Click(object sender, EventArgs e)
        {
            tbPageNumber.Text = (int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture) - 1).ToString(CultureInfo.InvariantCulture);
            Search();
        }

        /// <summary>
        /// Handles the Click event of the btnNextAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void NextAll_Click(object sender, EventArgs e)
        {
            tbPageNumber.Text = (int.Parse(tbPageNumber.Text, CultureInfo.InvariantCulture) + 1).ToString(CultureInfo.InvariantCulture);
            Search();
        }

        /// <summary>
        /// Handles the Click event of the btnNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Next_Click(object sender, EventArgs e)
        {
            tbPageNumber.Text = lblTotalPage.Text;
            Search();

        }
    }
}